package com.fhd.dao.House;

import com.fhd.dao.BaseDao;
import com.fhd.pojo.House;
import com.fhd.pojo.Merchant;
import com.mysql.cj.util.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class HouseDaoImplement implements HouseDao {
    // 获取某个商户的所有房型
    @Override
    public ArrayList<House> getSomeoneHouse(Connection conn, int merchantId) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        ArrayList<House> houses = new ArrayList<>();

        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "select * from house where merchant_id = ?";
                Object[] parameters = {merchantId};
                rs = BaseDao.executeQuery(conn, pstmt, rs, sql, parameters);

                while (rs.next()) {
                    House house = new House();
                    house.setId(rs.getInt("id"));
                    house.setMerchantId(rs.getInt("merchant_id"));
                    house.setHouseName(rs.getString("house_name"));
                    house.setPrice(rs.getInt("price"));
                    house.setRemainNumber(rs.getInt("remain_number"));
                    house.setKeyWord(rs.getString("key_word"));
                    houses.add(house);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,rs);
        }
        return houses;
    }

    // 增加房型
    @Override
    public int addHouse(Connection conn, int merchantId, String houseName, int price, int remainNumber, String keyWord) {
        PreparedStatement pstmt = null;
        int affectedRow = 0;
        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "insert into house (merchant_id,house_name,price,remain_number,key_word) values (?,?,?,?,?)";
                Object[] parameters = {merchantId,houseName,price,remainNumber,keyWord};
                affectedRow = BaseDao.executeUpdate(conn, pstmt,sql, parameters);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,null);
        }
        return affectedRow;
    }

    // 删去房型
    @Override
    public int deleteHouse(Connection conn, int id) {
        PreparedStatement pstmt = null;
        int affectedRow = 0;
        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "delete from house where id = ?";
                Object[] parameters = {id};
                affectedRow = BaseDao.executeUpdate(conn, pstmt,sql, parameters);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,null);
        }
        return affectedRow;
    }

    // 修改房型信息
    @Override
    public int updateHouse(Connection conn, int id, String houseName, int price, int remainNumber, String keyWord) {
        PreparedStatement pstmt = null;
        int affectedRow = 0;
        ArrayList<Object> objects = new ArrayList<>();
        try {
            if (conn != null) {
                // 通过前端传来的数据拼接sql语句,并把参数先放进ArrayList中
                StringBuffer sql = new StringBuffer("update house set ");
                if (!StringUtils.isNullOrEmpty(houseName)) {
                    sql.append("house_name = ?,");
                    objects.add(houseName);
                }
                if (price >= 0) {
                    sql.append("price = ?,");
                    objects.add(price);
                }
                if (remainNumber >= 0) {
                    sql.append("remain_number = ?,");
                    objects.add(remainNumber);
                }
                if (!StringUtils.isNullOrEmpty(keyWord)) {
                    sql.append("key_word = ?,");
                    objects.add(keyWord);
                }
                sql.deleteCharAt(sql.lastIndexOf(","));
                sql.append(" where id = ?");
                objects.add(id);

                Object[] parameters = objects.toArray();
                affectedRow = BaseDao.executeUpdate(conn, pstmt,sql.toString(),parameters);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,null);
        }
        return affectedRow;
    }

    // 减少剩余数量
    @Override
    public int updateRemainNumber(Connection conn, int id, int remainNumber) {
        PreparedStatement pstmt = null;
        int affectedRow = 0;
        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "update house set remain_number = remain_number + ? where id = ?";
                Object[] parameters = {remainNumber,id};
                affectedRow = BaseDao.executeUpdate(conn, pstmt,sql, parameters);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,null);
        }
        return affectedRow;
    }
}
